package com.hyz.service.permission.impl;

import client.AssetClient;
import com.hyz.model.dto.ApprovalResultRequest;
import com.hyz.model.vo.AssetImportResponse;
import com.hyz.model.vo.AssetOutboundResponse;
import com.hyz.service.permission.RepoManagerService;
import com.hyz.util.Conversion;
import com.hyz.util.GetUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pojo.ResponseResult;
import pojo.asset.assetImport.AssetImport;
import pojo.asset.assetImport.AssetImportResult;
import pojo.asset.assetOutbound.AssetOutbound;
import pojo.asset.assetOutbound.AssetOutboundResult;

import java.util.List;
import java.util.Objects;

/**
 * @author HGD
 * @date 2022/10/8 15:21
 */
@Service
public class RepoManagerServiceImpl implements RepoManagerService {
    @Autowired
    private AssetClient assetClient;
    @Autowired
    private Conversion conversion;
    @Autowired
    private GetUser getUser;
    private final int code = 200;

    @Override
    public ResponseResult<List<AssetImportResponse>> getAssetImports() {
        Long repoManagerId = getUser.getUser().getId();
        ResponseResult<List<AssetImport>> result = assetClient.getAssetImports(repoManagerId);
        if (result.getStatus() != code) {
            return new ResponseResult<>(result.getStatus(), result.getMsg());
        }
        List<AssetImportResponse> responses = conversion.getAssetImportResponse(result.getData());
        return new ResponseResult<>(result.getStatus(), result.getMsg(), responses);
    }

    @Override
    public ResponseResult<List<AssetImportResponse>> updateAssetImport(ApprovalResultRequest request) {
        if (Objects.isNull(request.getApprovalId()) || Objects.isNull(request.getResult())) {
            return new ResponseResult<>(400, "参数缺失");
        }
        assetClient.updateAssetImport(request.getApprovalId(),
                request.getResult() ? AssetImportResult.SUCCEEDED : AssetImportResult.FAILED);
        return getAssetImports();
    }


    @Override
    public ResponseResult<List<AssetOutboundResponse>> rmGetAssetOutbounds() {
        Long repoManagerId = getUser.getUser().getId();
        ResponseResult<List<AssetOutbound>> result = assetClient.rmGetAssetOutbounds(repoManagerId);
        if (result.getStatus() != code) {
            return new ResponseResult<>(result.getStatus(), result.getMsg());
        }
        List<AssetOutboundResponse> responses = conversion.getAssetOutboundResponse(result.getData());
        return new ResponseResult<>(result.getStatus(), result.getMsg(), responses);
    }

    @Override
    public ResponseResult<List<AssetOutboundResponse>> rmUpdateAssetOutbound(ApprovalResultRequest request) {
        if (Objects.isNull(request.getApprovalId()) || Objects.isNull(request.getResult())) {
            return new ResponseResult<>(400, "参数缺失");
        }
        ResponseResult<String> responseResult = assetClient.rmUpdateAssetOutbound(request.getApprovalId(), request.getResult() ? AssetOutboundResult.SUCCEEDED : AssetOutboundResult.FAILED);
        if (responseResult.getStatus() != code) {
            return new ResponseResult<>(responseResult.getStatus(), responseResult.getMsg());
        }
        return rmGetAssetOutbounds();
    }

}
